package org.fuxian.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fuxian.dao.VehicleMapper;
import org.fuxian.model.ServiceNetwork;
import org.fuxian.model.Vehicle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/vehicle")
public class VehicleController {

    @Autowired
    VehicleMapper vehicleMapper;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", "200");

        PageHelper.startPage(page, 5);
        List<Vehicle> imgList = vehicleMapper.selectAll();
        PageInfo<Vehicle> pageInfo = new PageInfo<>(imgList, 5);
        returnMap.put("pageInfo", pageInfo);
        return returnMap;
    }

    @RequestMapping("/getOneById")
    @ResponseBody
    public Map<String, Object> getOne(@RequestParam(value = "id")Integer id) {
        Map<String, Object> map = new HashMap<>();
        Vehicle vehicle = vehicleMapper.selectByPrimaryKey(id);

        if (vehicle != null) {
            map.put("code", "200");
            map.put("vehicle", vehicle);
        } else {
            map.put("code", "500");
        }
        return map;
    }



}
